package com.thjk.bdf.system.http.service.impl;

import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thjk.bdf.common.constants.ResidsConstant;
import com.thjk.bdf.common.utils.OkHttpUtil;
import com.thjk.bdf.common.utils.RedisUtils;
import com.thjk.bdf.common.utils.StringUtils;
import com.thjk.bdf.system.http.request.BdOrderQueryRequest;
import com.thjk.bdf.system.http.response.BdGetTokenResponse;
import com.thjk.bdf.system.http.response.BdOrderQueryResponse;
import com.thjk.bdf.system.http.response.vo.BdGetTokenData;
import com.thjk.bdf.system.http.response.vo.BdOrderData;
import com.thjk.bdf.system.http.service.OrderService;
import com.thjk.bdf.system.service.config.BdFConfig;

@Service
public class OrderServiceImpl implements OrderService{
	
	private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
	
	@Autowired
	private RedisUtils redisUtils;
	
	@Autowired
	private BdFConfig bdFConfig;

	@Override
	public String getBdToken() {
		//先获取token
		//先判断本地token是否还存在
		String key = ResidsConstant.ORDER_QUERY_CODE;
		String value = (String) redisUtils.get(key);
		if(StringUtils.isEmpty(value)){
			//缓存中没有数据，调用接口获取token
			Map<String, Object> jsonParamMap = new LinkedHashMap<>();
			jsonParamMap.put("appKey", bdFConfig.getAppKey());
			jsonParamMap.put("secretKey", bdFConfig.getSecretKey());
			//此字段不传，默认3600s
			//jsonParamMap.put("validSeconds", "");
			JSONObject jsonParams = new JSONObject(jsonParamMap);
			Map<String, Object> headParamMap = new LinkedHashMap<>();
			headParamMap.put("channel", bdFConfig.getChannel());
			String responseBody = OkHttpUtil.postJsonParams(bdFConfig.getGetTokenUrl(), jsonParams.toJSONString(), headParamMap);
			logger.info("get token result:" +responseBody);
			if(StringUtils.isNotEmpty(responseBody)) {
				BdGetTokenResponse bdGetTokenResponse = JSON.parseObject(responseBody, BdGetTokenResponse.class);
				if("200".equals(bdGetTokenResponse.getCode()) && bdGetTokenResponse.getSuccess()) {
					BdGetTokenData bdGetTokenData = bdGetTokenResponse.getData();
					value = bdGetTokenData.getToken();
					redisUtils.set(key, value, bdFConfig.getTokenOutTime());
				}
			}
		}
		return value;
	}

	@Override
	public BdOrderData getBdOrderInfo(BdOrderQueryRequest bdOrderQueryRequest) {
		BdOrderData bdOrderData = null;
		//调用订单查询接口查询
		Map<String, Object> headParamMap = new LinkedHashMap<>();
		headParamMap.put("token", bdOrderQueryRequest.getToken());
		logger.info("调用大数订单查询接口start............");
		logger.info("请求流水号：" + bdOrderQueryRequest.getApplicationNum());
		String responseBody = OkHttpUtil.getForHeader(bdFConfig.getOrderQueryUrl() + bdOrderQueryRequest.getApplicationNum(), null, headParamMap);
		logger.info("返回参数：" + responseBody);
		logger.info("调用大数订单查询接口end............");
		if(StringUtils.isNotEmpty(responseBody)) {
			BdOrderQueryResponse bdOrderQueryResponse = JSON.parseObject(responseBody, BdOrderQueryResponse.class);
			if("200".equals(bdOrderQueryResponse.getCode()) && bdOrderQueryResponse.getSuccess()) {
				bdOrderData = bdOrderQueryResponse.getData();
			}else {
				logger.error("orderInfo query error:"+bdOrderQueryResponse.getMessage());
			}
		}
		return bdOrderData;
	}

}
